package com.hss.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.hss.base.interfaces.IBaseView;
import com.hss.base.util.StartActivityUtil;

/**
 * Created by Administrator on 2017/2/10.
 */

public abstract class BaseFragment extends Fragment implements IBaseView{
    MaterialDialog lodingDilog= null;//loading dialog
    boolean isShowing = false;// 判断fragment 是否显示

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        //与viewpager 使用时可判断是否显示
//        isShow = isVisibleToUser;
    }

    @Override
    public void onResume() {
        super.onResume();
        isShowing = true;
    }
    //判断Fragment是显示还是隐藏，但是第一次显示要在onResume()里判断。
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        isShowing = !hidden;
    }

    @Override
    public void showLoadingDialog() {
        if (!isShowing || getActivity() == null || getActivity().isFinishing()){
            return;
        }
        lodingDilog =   new MaterialDialog.Builder(getActivity())
                .content(R.string.loading)
                .progress(true, 0)
                .show();
    }

    @Override
    public void hideLoadingDialog() {
            if (lodingDilog !=null){
                lodingDilog.dismiss();
            }
    }



    @Override
    public void showToast(String text) {
        Toast.makeText(getActivity().getApplicationContext(),text,Toast.LENGTH_SHORT).show();
    }


    @Override
    public void showToast(int textId) {
        Toast.makeText(getActivity().getApplicationContext(),textId,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void destoryRes() {

    }


    @Override
    public <T extends AppCompatActivity> void startActy(Class<T> clazz) {
        StartActivityUtil.startActivity(clazz, this);
    }

    @Override
    public <T extends AppCompatActivity> void startActy(Class<T> clazz, Bundle data) {
        StartActivityUtil.startActivity(clazz,data, this);

    }

    @Override
    public <T extends AppCompatActivity> void startActyForResult(Class<T> clazz, Bundle data, int requestCode) {
        StartActivityUtil.startActivityForResult(clazz,requestCode,data, this);

    }

    @Override
    public <T extends AppCompatActivity> void startActyForResult(Class<T> clazz, int requestCode) {
        StartActivityUtil.startActivityForResult(clazz,requestCode, this);

    }


    @Override
    public void finishActivity() {
        getActivity().finish();
    }

    @Override
    public void finishActivity(int resultCode) {
        getActivity().setResult(resultCode);
        getActivity().finish();
    }

    @Override
    public void finishActivity(int resultCode, Intent data) {
        getActivity().setResult(resultCode,data);
        getActivity().finish();
    }
}
